
#ifndef __KEY_TABLE_VIEW_H
#define __KEY_TABLE_VIEW_H

#define KEY_TABLE_USE_TYPE__ENCRYPTION 0
#define KEY_TABLE_USE_TYPE__DECRYPTION 1
#define KEY_TABLE_USE_TYPE__SIGNING    2
#define KEY_TABLE_USE_TYPE__CHECKING   3

#define KEY_OPEN_ERROR__NO_ERROR          0
#define KEY_OPEN_ERROR__INVALID_KEY_NAME  1
#define KEY_OPEN_ERROR__INVALID_KEY_DST   2
#define KEY_OPEN_ERROR__KEY_NOT_FOUND     3
#define KEY_OPEN_ERROR__FAILED_SANITY     4
#define KEY_OPEN_ERROR__KEY_IMPORT_FAILED 5
#define KEY_OPEN_ERROR__KEY_NULL          6

#define KEY_INSERT_ERROR__NO_ERROR         0
#define KEY_INSERT_ERROR__INVALID_KEY_NAME 1
#define KEY_INSERT_ERROR__INVALID_KEY_SRC  2
#define KEY_INSERT_ERROR__EXPORT_ERROR     3

#include "../crypt/keys.h"

class NewKeyDB;

class KeyTableView
{
   public:
      KeyTableView(NewKeyDB *newkdb, int ikeytableusetype,
                   const char *itablename, const char *itableWIPpwd,
                   const char *iresidentkeyname, const char *iresidentkeypwd);
      ~KeyTableView();
      int CountKeyChainOfOwner(const char *owner);
      char *GetLatestKeyNameOfOwner(const char *owner);
      char GetMarkOfKey(const char *keyname);
      void SetMarkOfKey_unsafe(const char *keyname, char newmark);
      void RetryGettingOwnKeys(const char *ownpwd);
      int TryToInsertKeyIntoTable(const char *name, const char *pwd, char sig, const char *owner, Key *key);
      int TryToInsertKeyIntoTable_tsafe(const char *name, const char *pwd, char sig, const char *owner, Key *key, int uselock);
      void TryToInsertKeyErrorCheck(const char *keyname, int terrno);
      int TryToGetKeyFromTable(const char *name, const char *pwd, Key **key);
      void TryToGetKeyErrorCheck(const char *keyname, int terrno);
      int GeneratingNewOwnKeysAcceptable() { return couldgeneratekeysflag; }
   protected:
      NewKeyDB *nkeydb;
      int keytableusetype;
      char *tablename;
      char *tableWIPpwd;
      char *residentkeyname;
      Key *ownkey;
      int couldgeneratekeysflag;
};

// Could add EncryptionKeyTable, DecryptionKeyTable etc...

#endif
